package annotool.classify;
class Haar 
{
		int n=8;
		double denom=Math.sqrt(8);
		double a=Math.sqrt(2);

		
		int oriz=0,newz=0;
		double h[][]={{1,1,1,1,1,1,1,1},
			{1,1,1,1,-1,-1,-1,-1},
			{a,a,-a,-a,0,0,0,0},
			{0,0,0,0,a,a,-a,-a},
			{2,-2,0,0,0,0,0,0},
			{0,0,2,-2,0,0,0,0},
			{0,0,0,0,2,-2,0,0},
			{0,0,0,0,0,0,2,-2}};
		Haar()
		{
		for(int i=0;i<8;i++)
			for(int j=0;j<8;j++)
				h[i][j]=h[i][j]/denom;
		}
		
		int[][] forwardTransform(int pixels[][])
		{
			int aw[][]=new int[8][8];
			int waw[][]=new int[8][8];
			for(int i=0;i<8;i++)
			{
				for(int j=0;j<8;j++)
				{
					double sum=0;
					for(int k=0;k<8;k++)
						sum=sum+pixels[i][k]*h[k][j];
					aw[i][j]=(int)sum;
				}
			}
			int th=0;
			for(int i=0;i<8;i++)
			{
				for(int j=0;j<8;j++)
				{
					double sum=0;
					for(int k=0;k<8;k++)
						sum=sum+h[k][i]*aw[k][j];
					waw[i][j]=(int)sum;
					th=th+(int)sum;
				}
			}
			th=(int)th/64;
			
			for(int i=0;i<8;i++)
			{
				for(int j=0;j<8;j++)
				{
					if(Math.abs(waw[i][j])<th)
									waw[i][j]=0;
					if(pixels[i][j]==0)
						oriz++;
					if(waw[i][j]==0)
						newz++;
				//	//System.out.print(waw[i][j]+"  ");
				}
				////System.out.println("");
			}
		////System.out.println("Compression Ratio is : "+ (64-oriz)/(64-newz));
			
			return(waw);
		}
		int compresseddata()
	{
			return(newz);
	}

		int[][] inverseTransform(int waw[][])
		{
		
		int aw[][]=new int[8][8];
			for(int i=0;i<8;i++)
			{
				for(int j=0;j<8;j++)
				{
					double sum=0;
					for(int k=0;k<8;k++)
						sum=sum+h[i][k]*waw[k][j];
					aw[i][j]=(int)sum;
				}
			}

			for(int i=0;i<8;i++)
			{
				for(int j=0;j<8;j++)
				{
					double sum=0;
					for(int k=0;k<8;k++)
						sum=sum+aw[i][k]*h[j][k];
					waw[i][j]=(int)sum;
				}
			}

			

			
			return(waw);
		}
		public static void main( String args[])
	{
			Haar har=new Haar();
			int pixels[][]={{100,103,99,97,93,94,78,73},
			{102, 97, 100, 111, 113, 104, 96, 82},
			{99, 109, 104, 95, 93, 92, 88, 76},
			{114, 104, 99, 102, 93, 82, 74, 74},
			{96, 91, 91, 87, 79, 78, 77, 76},
			{90, 88, 83, 78, 77, 74, 76, 76},
			{92, 81, 73, 72, 69, 65, 66, 62},
			{75, 70 ,69, 65, 60, 55, 61, 65}};
			int quant[][]={{8, 7, 8, 8, 34, 34, 34, 34,},
				{7, 7, 8, 8, 34, 34, 34, 34,},
				{8, 8, 12, 12, 34, 34, 34, 34,},
				{8,8, 12, 12, 34, 34, 34, 34},
				{ 34, 34, 34, 34,55, 55, 55 ,55},
				{34, 34 ,34 ,34, 55, 55, 55, 55},
				{34 ,34 ,34 ,34 ,55 ,55 ,55 ,55 },
				{34, 34, 34, 34, 55, 55, 55, 55}}; 
			pixels=har.forwardTransform(pixels);
			for(int i=0;i<8;i++)
			{
				for(int j=0;j<8;j++)
				{
					pixels[i][j]=(int)(pixels[i][j]/quant[i][j]);
				}//System.out.println(" ");
			}
		
			for(int i=0;i<8;i++)
			{
				for(int j=0;j<8;j++)
				{
					//System.out.print(pixels[i][j]+"  ");
				}//System.out.println(" ");
			}

			for(int i=0;i<8;i++)
			{
				for(int j=0;j<8;j++)
				{
					pixels[i][j]=pixels[i][j]*quant[i][j];;
				}//System.out.println(" ");
			}


			pixels=har.inverseTransform(pixels);

	}


}
